//
//  MOFSPickerView.m
//  MOFSPickerManager
//
//  Created by luoyuan on 16/8/30.
//  Copyright © 2016年 luoyuan. All rights reserved.
//

#import "MOFSPickerView.h"

#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MOFSPickerView() <UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) NSMutableDictionary *recordDic;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, assign) NSInteger selectedRow;

@end

@implementation MOFSPickerView

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (NSMutableDictionary *)recordDic {
    if (!_recordDic) {
        _recordDic = [NSMutableDictionary dictionary];
    }
    return _recordDic;
}

#pragma mark - create UI

- (instancetype)initWithFrame:(CGRect)frame {
    
    //[self initToolBar];    
    CGRect initialFrame;
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        self.backgroundColor = CSClearColor;
        
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        [self initPickerView];
        
        [self initContainerView];
        
        [self initBgView];
    }
    return self;
}

- (void)initToolBar {
    
    self.toolBar = [[MOFSToolbar alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 44)];
    
    self.toolBar.translucent = NO;
}

- (void)initContainerView {
    self.containerView = [[UIView alloc] init];
    self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.containerView.userInteractionEnabled = YES;
    [self.containerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenWithAnimation)]];
    [self addSubview:self.containerView];
    [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

- (void)initBgView {
    
    //白色背景
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = CSWhiteColor;
    [self addSubview:self.bgView];
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = BoldBigFont;
    [self.bgView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView).offset(20);
        make.centerX.mas_equalTo(self.bgView);
    }];
    
    UIButton *closeBtn = [[UIButton alloc] init];
    [closeBtn setImage:@"pop_icon_close"];
    [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
    [self.bgView addSubview:closeBtn];
    self.closeBtn = closeBtn;
    [closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bgView).offset(-10);
        make.top.mas_equalTo(self.bgView).offset(10);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = LineBackGroundColor;
    [self.bgView addSubview:lineView];
    self.lineView = lineView;
    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(30);
        make.left.width.mas_equalTo(self.bgView);
        make.height.mas_equalTo(1);
    }];
    
    [self.bgView addSubview:self.pickerView];
    [self.pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.left.width.mas_equalTo(self.bgView);
        make.height.mas_equalTo(200);
    }];
    
    MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
    [self.bgView addSubview:bottomView];
    self.bottomView = bottomView;
    [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pickerView.mas_bottom);
        make.left.width.bottom.mas_equalTo(self.bgView);
    }];
    
    UILabel *zhangLabel = [[UILabel alloc] init];
    zhangLabel.font = SmallFont;
    zhangLabel.hidden = YES;
    zhangLabel.text = @"张";
    [self.bgView addSubview:zhangLabel];
    self.zhangLabel = zhangLabel;
    [zhangLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pickerView);
        make.left.mas_equalTo(self.pickerView.mas_centerX).offset(20);
    }];
}

-(void)initPickerView{
    
    // 初始化pickerView
    self.pickerView = [[UIPickerView alloc]init];
    //[self.bgView addSubview:self.pickerView];
    
    //指定数据源和委托
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
}

#pragma mark - Action

- (void)showMOFSPickerViewWithDataArray:(NSArray *)array commitBlock:(void(^)(NSString *string))commitBlock cancelBlock:(void(^)(void))cancelBlock {
    
    self.dataArr = [NSMutableArray arrayWithArray:array];
    
    [self.pickerView reloadAllComponents];
    
    self.selectedRow = self.dataArr.count / 2;
    
    NSString *tagStr = [NSString stringWithFormat:@"%ld",(long)self.showTag];
    if ([self.recordDic.allKeys containsObject:tagStr]) {
        self.selectedRow = [self.recordDic[tagStr] integerValue];
    }
    [self.pickerView selectRow:self.selectedRow inComponent:0 animated:NO];
    
    if ([self.titleLabel.text isEqualToString:@"选择每个会员限购张数"]) {
        
        self.zhangLabel.hidden = NO;
    }
    else{
        
        self.zhangLabel.hidden = YES;
    }
    
    [self showWithAnimation];
    
    __weak __typeof(self) weakSelf = self;
    self.bottomView.cancelBlock = ^ {
        if (cancelBlock) {
            
            [weakSelf hiddenWithAnimation];
            
            cancelBlock();
        }
    };
    
    self.bottomView.commitBlock = ^ {
        [weakSelf hiddenWithAnimation];
        if (commitBlock) {
            
            NSString *rowStr = [NSString stringWithFormat:@"%ld",(long)weakSelf.selectedRow];
            [weakSelf.recordDic setValue:rowStr forKey:tagStr];
            commitBlock(weakSelf.dataArr[weakSelf.selectedRow]);
        }
    };
}

- (void)showWithAnimation {
    [self addViews];
    self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    CGFloat height = self.bgView.frame.size.height;
    self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT + height / 2);
    [UIView animateWithDuration:0.25 animations:^{
        self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT - height / 2);
        self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    CGFloat height = self.bgView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT + height / 2);
        self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
    
    //[window addSubview:self.bgView];
    //[self.bgView addSubview:self.toolBar];
    //[self.bgView addSubview:self];
}

- (void)hiddenViews {
    [self removeFromSuperview];
    //[self.toolBar removeFromSuperview];
    //[self.bgView removeFromSuperview];
    //[self.containerView removeFromSuperview];
}

#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.dataArr[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectedRow = row;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 50.0f;
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    
    if (!pickerLabel){
        
        pickerLabel = [[UILabel alloc] init];
        
        // Setup label properties - frame, font, colors etc
        
        //adjustsFontSizeToFitWidth property to YES
        
        //pickerLabel.adjustsFontSizeToFitWidth = YES;
        
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        
        [pickerLabel setFont:BigFont];
    }
    
    // Fill the label text here
    
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
    
}

@end
